/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.managedbean;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import mrp.productionPlanning.entity.MPSTableEntity;
import mrp.productionPlanning.session.MPSTableSessionBean;
import mrp.productionPlanning.session.MonthlyDetailSessionBean;

/**
 *
 * @author Wei Haoyang
 */
@ManagedBean
@ViewScoped
public class CreateMPSTableManagedBean {

    private int yearName;
    @EJB
    private MPSTableSessionBean mpsTableSessionBean = new MPSTableSessionBean();
    @EJB
    private MonthlyDetailSessionBean mdsb = new MonthlyDetailSessionBean();

    /** Creates a new instance of CreateMPSTableManagedBean */
    public CreateMPSTableManagedBean() {
    }

    public void create(ActionEvent event) throws IOException {
        List<MPSTableEntity> check = mpsTableSessionBean.getAllMPSTableEntityByProduct("productA", yearName);
        if (check.size() == 0){
            List<MPSTableEntity> mpsListA = new ArrayList<MPSTableEntity>();
            List<MPSTableEntity> mpsListB = new ArrayList<MPSTableEntity>();
            List<MPSTableEntity> mpsListC = new ArrayList<MPSTableEntity>();
            List<MPSTableEntity> mpsListD = new ArrayList<MPSTableEntity>();
            SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
            Calendar cal = new GregorianCalendar();
            cal.set(yearName, 0, 1, 0 , 1, 0);
            Date d = new Date();
            d = cal.getTime();
    //        String temp = sdf.format(cal.getTime());
    //        try{
    //            d = sdf.parse(temp);
    //        }
    //        catch(ParseException ex){
    //        
    //        }
            System.out.println("First:"+ d.toString());

            int i = 0;
            while (i < 12) {
                System.out.println("***This is the original time:" + d.toString());
                System.out.println("----------------------------------Check Point A----------------------------------");
                mpsListA.add(mpsTableSessionBean.createMPSTableA(d));
                System.out.println("----------------------------------Check Point B----------------------------------");
                mpsListB.add(mpsTableSessionBean.createMPSTableB(d));
                System.out.println("----------------------------------Check Point C----------------------------------");
                mpsListC.add(mpsTableSessionBean.createMPSTableC(d));
                System.out.println("----------------------------------Check Point D----------------------------------");
                mpsListD.add(mpsTableSessionBean.createMPSTableD(d));
                cal.add(Calendar.MONTH, 1);
                d = cal.getTime();
                i++;
            }

            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mpsListA", mpsListA);
            System.out.println("ListASize:"+mpsListA.size());
            System.out.println("FirstATime"+mpsListA.get(0).getMonth().toString());
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mpsListB", mpsListB);
            System.out.println("ListASize:"+mpsListB.size());
            System.out.println("FirstBTime"+mpsListB.get(0).getMonth().toString());
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mpsListC", mpsListC);
            System.out.println("ListASize:"+mpsListC.size());
            System.out.println("FirstCTime"+mpsListC.get(0).getMonth().toString());
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mpsListD", mpsListD);
            System.out.println("ListASize:"+mpsListD.size());
            System.out.println("FirstDTime"+mpsListD.get(0).getMonth().toString());
            System.out.println("---------------------------------------End Of Generation---------------------------------------");
            FacesContext.getCurrentInstance().getExternalContext().redirect("editMPSTable.xhtml");
        }
        else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "The MPS for the indicated year is already created", ""));
        }
    }
    
    public void jump(ActionEvent event) throws IOException{
        List<MPSTableEntity> mpsListA = new ArrayList<MPSTableEntity>();
        List<MPSTableEntity> mpsListB = new ArrayList<MPSTableEntity>();
        List<MPSTableEntity> mpsListC = new ArrayList<MPSTableEntity>();
        List<MPSTableEntity> mpsListD = new ArrayList<MPSTableEntity>();
//        SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
//        Calendar cal = new GregorianCalendar();
//        cal.set(yearName, 0, 1, 0 , 1, 0);
//        Date d = new Date();
//        d = cal.getTime();
//        int correction = cal.get(Calendar.MONTH);
//        String temp = sdf.format(cal.getTime());
//        try{
//            d = sdf.parse(temp);
//        }
//        catch(ParseException ex){
//        
//        }
//        cal.setTime(d);
//        cal.set(Calendar.MONTH, correction);
//        d = cal.getTime();
//        System.out.println("Date require:" + d.toString());
        
        mpsListA = mpsTableSessionBean.getAllMPSTableEntityByProduct("productA",yearName);
        mpsListB = mpsTableSessionBean.getAllMPSTableEntityByProduct("productB",yearName);
        mpsListC = mpsTableSessionBean.getAllMPSTableEntityByProduct("productC",yearName);
        mpsListD = mpsTableSessionBean.getAllMPSTableEntityByProduct("productD",yearName);
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mpsListA", mpsListA);
        System.out.println("ListASize:"+mpsListA.size());
        System.out.println("FirstATime"+mpsListA.get(5).getMonth().toString());
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mpsListB", mpsListB);
        System.out.println("ListASize:"+mpsListB.size());
        System.out.println("FirstBTime"+mpsListB.get(5).getMonth().toString());
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mpsListC", mpsListC);
        System.out.println("ListASize:"+mpsListC.size());
        System.out.println("FirstCTime"+mpsListC.get(5).getMonth().toString());
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mpsListD", mpsListD);
        System.out.println("ListASize:"+mpsListD.size());
        System.out.println("FirstDTime"+mpsListD.get(5).getMonth().toString());
        System.out.println("---------------------------------------End Of Generation---------------------------------------");
        FacesContext.getCurrentInstance().getExternalContext().redirect("editMPSTable.xhtml");
    }

    public int getYearName() {
        return yearName;
    }

    public void setYearName(int yearName) {
        this.yearName = yearName;
    }

    public MPSTableSessionBean getMpsTableSessionBean() {
        return mpsTableSessionBean;
    }

    public void setMpsTableSessionBean(MPSTableSessionBean mpsTableSessionBean) {
        this.mpsTableSessionBean = mpsTableSessionBean;
    }
}
